using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallColour : MonoBehaviour
{
    public enum Colour { None, White, Black, Red, Yellow };
    public Colour colour;
    public AudioSource clickNoise;

    // Use this for initialization
    void Start()
    {
        var sauces = FindObjectsOfType<AudioSource>();
        foreach(var sauce in sauces)
        {
            if (sauce.clip.name == "click3r")
            {
                clickNoise = sauce;
                break;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (transform.position.sqrMagnitude >= 25.0)
        {
            Cue c = gameObject.GetComponent<Cue>();
            CueMulti cm = gameObject.GetComponent<CueMulti>();
            if (c)
            {
                c.Removed();
                GameState game = FindObjectOfType<GameState>();
                game.SetPenaltyText("Chipped the cue ball off the table. 2 shot penalty.");

            }
            else if (cm)
            {
                cm.Removed();
                GameStateMulti gameMulti = FindObjectOfType<GameStateMulti>();
                gameMulti.SetPenaltyText("Chipped the cue ball off the table. 2 shots.");
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }

    void OnDestroy()
    {
        GameState game = FindObjectOfType<GameState>();
        if (game) game.Potted(colour);
        GameStateMulti gamemulti = FindObjectOfType<GameStateMulti>();
        if (gamemulti) gamemulti.Potted(colour);
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        var otherBall = collision.gameObject.GetComponent<BallColour>();
        if (otherBall)
        {
            clickNoise.PlayOneShot(clickNoise.clip, collision.relativeVelocity.magnitude / 8.0f);
        }
    }
}
